package com.example.blog.dao;

import com.example.blog.pojo.blog;
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Mapper
@Component
public interface blogRepository {

    @Select("select blog.*,type.id as tid,type.name from blog.blog,blog.type where type_id = type.id and blog.id = #{id}")
    @ResultMap({"blog"})
    public blog findById(Integer id);

    @Select("select blog.*,type.id as tid,type.name from blog.blog,blog.type where type_id = type.id and blog.title like '${title}'")
    @ResultMap("blog")
    public List<blog> findByTitle(String title);

    @Select("select blog.*,type.id as tid,type.name from blog.blog,blog.type where type_id = type.id")
    @ResultMap("blog")
    public List<blog> findAll();

    @Delete("delete from blog.blog where id = #{id}")
    public void deleteById(Integer id);

    @Insert("insert into blog.blog" +
            "(id ,title ,content ,firstPicture ,flag ,view ,aprreciation ,shareStatement ,commentabled ,published ,recommened ,creatTime ,type_id) values " +
            "(#{id} ,#{title} ,#{content} ,#{firstPicture} ,#{flag} ,#{view} ,#{aprreciation} ,#{shareStatement} ,#{commentabled} ,#{published} ,#{recommened} ,#{creatTime} ,#{type_id})")
    public void save(blog blog);

    @Update("update blog.blog set " +
            "title=#{title} ,content=#{content} ,firstPicture=#{firstPicture} ,flag=#{flag} ,view=#{view} ," +
            "aprreciation=#{aprreciation} ,shareStatement=#{shareStatement} ,commentabled=#{commentabled} ," +
            "published=#{published} ,recommened=#{recommened} ,updateTime=#{updateTime} , type_id=#{type_id} where id= #{id}")
    public void update(blog blog);

    @Select("select blog.*,type.id as tid,type.name from blog.blog,blog.type " +
            "where type_id = type.id and blog.recommened = #{recommend} and blog.title like '${title}'")
    @ResultMap("blog")
    List<blog> findByTitleAndRecommend(String title, boolean recommend);

    @Select("select blog.*,type.id as tid,type.name from blog.blog,blog.type " +
            "where type_id = type.id and blog.recommened = #{recommend} and type.name = #{type} and blog.title like '${title}'")
    @ResultMap("blog")
    List<blog> findByTitleAndTypeAndRecommend(String title, String type, boolean recommend);

    @Select("SELECT blog.*,type.id as tid,type.name from blog.blog,blog.type WHERE type.name = #{type} AND type_id = type.id")
    @ResultMap("blog")
    List<blog> findByType(String type);

    @Select("select blog.*,tag.id as tagid,tag.`name` as tagname FROM blog,blog_tag,tag WHERE blog_tag.blog_id =blog.id AND blog_tag.tag_id = tag.id AND tag.id = #{tag}")
    @ResultMap("blog")
    List<blog> findByTag(Integer tag);

    /*@Select("select blog.*,tag.id as tagid,tag.`name` as tagname FROM blog,blog_tag,tag WHERE blog_tag.blog_id =blog.id AND blog_tag.tag_id = tag.id AND blog.id = #{id}")
    @ResultMap("blog")
    List<blog> findAllAndTag(Integer id);*/
}
